package com.yan.deezer.activity;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yan.deezer.R;
import com.yan.deezer.object.Album;
import com.yan.deezer.object.Artist;


public class ArtistAlbumListAdapter extends ArrayAdapter<Album> implements OnItemClickListener
{
	private static final String TAG = "ARTIST'S_ALBUM_LIST_ADAPTER";

	protected LayoutInflater mInflater;
	protected List<Album> mList;
	protected int mnViewResId;
	protected Album currentAlbum;
	protected DataManager mDataManager;
	protected Context mContext;
	protected AlbumViewHolder holder ;
	
	
	public ArtistAlbumListAdapter(Context context, List<Album> list, int resId) 
	{
		super(context, resId, list);
		mInflater = LayoutInflater.from(context);
		mList = list;
		mnViewResId = resId;
		mDataManager = DataManager.getInstance();
		mContext = context;
	}

	@Override
	public int getCount() 
	{
		int size = 0;
		if(mList != null)
		{
			size = mList.size();
		}
		return size;
	}
	
	@Override
	public long getItemId(int position) 
	{
		return position;
	}


	static class AlbumViewHolder
	{

		/*
		 * 
		 * 
		 * { "id": "27", "name": "Daft Punk", "link":
		 * "http://www.deezer.com/music/daft-punk", "picture":
		 * "http://api.deezer.com/2.0/artist/27/image", "nb_album": "48", "nb_fan":
		 * "271184", "radio": true, "type": "artist" }
		 */
		
		ImageView albumCover;
		TextView albumTitle;

	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		holder=null;
		
		if(convertView == null)
		{
			convertView = mInflater.inflate(mnViewResId, null);
			holder = new AlbumViewHolder();			
			holder.albumCover = (ImageView) convertView.findViewById (R.id.album_cover);
			holder.albumTitle = (TextView) convertView.findViewById (R.id.album_title);
	        
			convertView.setTag(holder);
		}
		else 
		{
			holder = (AlbumViewHolder) convertView.getTag();
		}

		//On associe les donnes avec le holder correspondant
		currentAlbum = getItem(position);
		
		//holder.articleTrailerClickZone.setTag(mCurrentarticle.getTrailer());
		
		int id=Integer.parseInt(currentAlbum.getId());
		
		holder.albumCover.setTag(id);
		
		mDataManager.setBitmapToImageView(currentAlbum.getCover(), holder.albumCover, id);
		
		
		holder.albumTitle.setText(currentAlbum.getTitle());
		holder.albumTitle.setSelected(true);
		holder.albumTitle.setCompoundDrawablePadding(3); 
		
		//holder.g.setTag(currentArtist.getLink());
		//mDataManager.setImageListToGallery(currentArtist.getLink(), holder.g, currentArtist, mContext);
	
			//starring = new SpannableString(mCurrentarticle.getPubDate().toGMTString());
			//starring.setSpan(new StyleSpan(Typeface.BOLD), 0, mCurrentarticle.getPubDate().toGMTString().length(), 0);
		
		
		//holder.articleStarring.setText(starring);
		
		// Set the adapter to our custom adapter (below)
	
		
		//On retourne la vue
		return convertView;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
	{
		Album album = getItem(position);
		if(album != null);
			
			
	}



		
	}

    
	
	

